package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author admin
 * @date 2024/9/21
 */
@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 查询今日运营数据
     */
    @Override
    public BusinessDataVO businessData(LocalDateTime beginTime, LocalDateTime endTime) {
        Map map = new HashMap();

        map.put("end", endTime);
        // 新增用户数量
        map.put("begin", beginTime);
        Integer newUsers = userMapper.getByMap(map);
        Integer orderCount = orderMapper.getByStatusMap(map);

        //     订单完成率
        map.put("status", Orders.COMPLETED);
        Integer validOrderCount = orderMapper.getByStatusMap(map);
        Double orderCompletionRate = 0.0;
        if(orderCount != 0) {
            orderCompletionRate = validOrderCount.doubleValue() / orderCount;
        }
    //     营业额
        Double turnover = orderMapper.sumByMap(map) == null ? 0.0 : orderMapper.sumByMap(map);
    //     平均客单价
        Double unitPrice = 0.0;
        if(validOrderCount != 0) {
            unitPrice = turnover.doubleValue() / validOrderCount;
        }
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
    }

    /**
     * 查询套餐总览
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Integer sold = setmealMapper.getByStatus(StatusConstant.ENABLE);
        Integer discontinued = setmealMapper.getByStatus(StatusConstant.DISABLE);
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询菜品总览
     */
    @Override
    public DishOverViewVO overviewDishes() {
        Integer sold = dishMapper.getByStatus(StatusConstant.ENABLE);
        Integer discontinued = dishMapper.getByStatus(StatusConstant.DISABLE);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询订单管理数据
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        // 获取今日时间
        LocalDate date = LocalDate.now();
        LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
        Integer allOrders = orderMapper.getByStatus(beginTime, endTime, null);
        Integer cancelledOrders = orderMapper.getByStatus(beginTime, endTime, Orders.CANCELLED);
        Integer completedOrders = orderMapper.getByStatus(beginTime, endTime, Orders.COMPLETED);
        Integer deliveredOrders = orderMapper.getByStatus(beginTime, endTime, Orders.CONFIRMED);
        Integer waitingOrders = orderMapper.getByStatus(beginTime, endTime, Orders.TO_BE_CONFIRMED);
        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }
}
